<?php

namespace app\api\controller;

use app\facade\Basic;
use think\Request;
use app\api\service\Index as IndexService;
use app\api\service\ProductMerge as ProductMergeService;
use app\api\service\CCNames as CCNamesService;
use app\api\service\Token;
use think\facade\Log;

class ProductMerge extends BaseController
{

    protected $service;
    public function __construct()
    {
        $this->service = new ProductMergeService();
    }

    public function getProductMergeId(Request $request)
    {
        $urlname = $request->param('urlname');
        $productMergeService = new ProductMergeService();

        $result = $productMergeService->getProductMergeId($urlname);
        return Basic::dataJson(0, 'common.success', $result);
    }




    /**
     * @describe
     * @author: Bobo
     * @date: 2025-04-16 11:40:47 
     * @param [type] @required
     * @return Renderable
     */
    public function getProductMergeList(Request $request)
    {
        $productMergeService = new ProductMergeService();
        $where = [];
        if ($request->has('brand') && $request->param('brand') > 0) {
            $where['brand'] = $request->param('brand');
        }
        if ($request->has('brand_name') && !empty($request->param('brand_name'))) {
            $where['brand_name'] = trim($request->param('brand_name'));
        }
        if ($request->has('accessory') && $request->param('accessory') > 0) {
            $where['accessory'] = $request->param('accessory');
        }
        if ($request->has('accessory_name') && !empty($request->param('accessory_name'))) {
            $where['accessory_name'] = trim($request->param('accessory_name'));
        }
        if ($request->has('algorithm')  && $request->param('algorithm') > 0) {
            $where['algorithm'] = $request->param('algorithm');
        }
        if ($request->has('algorithm_name')  && !empty($request->param('algorithm_name'))) {
            $where['algorithm_name'] = trim($request->param('algorithm_name'));
        }
        if ($request->has('coin')  && $request->param('coin') > 0) {
            $where['coin'] = $request->param('coin');
        }
        if ($request->has('coin_name')  && !empty($request->param('coin_name'))) {
            $where['coin_name'] = trim($request->param('coin_name'));
        }
        if ($request->has('solution')  && $request->param('solution') > 0) {
            $where['solution'] = $request->param('solution');
        }
        if ($request->has('solution_name')  && !empty($request->param('solution_name'))) {
            $where['solution_name'] = trim($request->param('solution_name'));
        }
        if ($request->has('max_price') && $request->param('max_price') > 0) {
            $where['max_price'] = $request->param('max_price');
        }
        if ($request->has('min_price') && $request->param('min_price') > 0) {
            $where['min_price'] = $request->param('min_price');
        }
        if ($request->has('sort') && $request->param('sort') > 0) {
            $where['sort'] = $request->param('sort');
        }
        if ($request->has('name') && !empty($request->param('name'))) {
            $where['name'] = trim($request->param('name'));
        }
        if ($request->has('type') && $request->param('type') > 0) {
            $where['type'] = $request->param('type');
        }

        if ($request->has('page') && $request->param('page') > 0) {
            $page = $request->param('page');
        } else {
            $page = 1;
        }
        if ($request->has('page_size') && $request->param('page_size') > 0) {
            $pageSize = $request->param('page_size');
        } else {
            $pageSize = 10;
        }
        $role = 1;
        $token = $request->header('x-api-key');
        if ($token) {
            // 验证 Token
            $tokenObj = new Token();
            $obj = $tokenObj->check($token);
            if ($obj) {
                $obj = json_decode($obj, true);
                $role = $obj['role'] == 'distributor' ? 2 : 1;
            }
        }
        $lang = $request->header('think-lang') ?? 'en';

        $result = $productMergeService->getProductMergeList($where, $role, $lang, $page, $pageSize);
        return Basic::dataJson(0, 'common.success', $result);
    }

    /**
     * @describe
     * @author: Bobo
     * @date: 2025-04-16 11:40:53 
     * @param [type] @required
     * @return Renderable
     */
    public function getProductMergeDetail(Request $request)
    {
        // 如果需要同时获取多个IP信息
        $ip = getRealIp($request);
        $productMergeService = new ProductMergeService();
        $where = $request->param();
        $ccNamesService = new CCNamesService();
        $ccNameId = $ccNamesService->getCCNamesByIp($ip);
        $lang = $request->header('think-lang') ?? 'en';
        $result = $productMergeService->getProductMergeDetail($where, $ccNameId, $lang);
        return Basic::dataJson(0, 'common.success', $result);
    }
}
